package com.jarpackage.service;

import com.jarpackage.model.Dependency;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class BatchImportService {
    private static final Logger logger = LoggerFactory.getLogger(BatchImportService.class);

    public List<Dependency> parseDependencies(String input) {
        List<Dependency> dependencies = new ArrayList<>();
        if (input == null || input.trim().isEmpty()) {
            return dependencies;
        }

        String[] lines = input.split("\\n");
        for (String line : lines) {
            try {
                Dependency dependency = parseDependencyLine(line);
                if (dependency != null) {
                    dependencies.add(dependency);
                }
            } catch (Exception e) {
                logger.error("Failed to parse dependency line: " + line, e);
            }
        }
        return dependencies;
    }

    private Dependency parseDependencyLine(String line) {
        if (line == null || line.trim().isEmpty()) {
            return null;
        }

        String[] parts = line.trim().split(",");
        if (parts.length < 3) {
            logger.warn("Invalid dependency format: " + line);
            return null;
        }

        Dependency dependency = new Dependency();
        dependency.setGroupId(parts[0].trim());
        dependency.setArtifactId(parts[1].trim());
        dependency.setVersion(parts[2].trim());
        dependency.setType("jar");

        return dependency;
    }
}